package com.itlong.cloud.controller;

import com.alibaba.fastjson.JSONArray;
import com.itlong.cloud.POJO.DTO.smartcard.SmartCardSyncGetPullDataDTO;
import com.itlong.cloud.POJO.DTO.smartcard.SmartCardSyncMessageDTO;
import com.itlong.cloud.POJO.PO.SmartCardPullPO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.enumerate.SmartCardSyncDataTypeEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.SmartCardErrorCodeEnum;
import com.itlong.cloud.project.operate.IOperateProjectServiceClient;
import com.itlong.cloud.project.smartcard.ISmartCardSyncDataServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <desc>
 *      与旺龙一卡通对接聚合Controller
 * </desc>
 *
 * @createDate 2017/10/27
 */
@RestController
@RequestMapping("/smartCard/syncdata/")
public class SmartCardBaseController extends BaseController{
    private static Logger LOG = LoggerFactory.getLogger(SmartCardBaseController.class);

    //项目信息服务接口协议提供对象
    @Autowired
    IOperateProjectServiceClient iOperateProjectServiceClient;

    //与旺龙一卡通对接服务接口协议提供对象
    @Autowired
    ISmartCardSyncDataServiceClient iSmartCardSyncDataServiceClient;


    /**
    * <desc>
    *     初步处理一卡通推送给云端的数据（放入相应的push表中并在变化标记表中登记）.
    * </desc>
    *
    * @param smartCardSyncMessageDTO 卡通云端同步消息
    * @return xx
    * @author LewisLouis
    * @createDate 2017-12-20
    */
    public MessageVO receiveSyncDataFromSmartCard(SmartCardSyncMessageDTO smartCardSyncMessageDTO) throws Exception {
        if (null == smartCardSyncMessageDTO){
            return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
        }
        //参数判断
        if (StringUtils.isBlank(smartCardSyncMessageDTO.getProjectId())){
            return new MessageVO(SmartCardErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
        }
        if (null == smartCardSyncMessageDTO.getDataType()){
            return new MessageVO(SmartCardErrorCodeEnum.DATA_TYPE_ERR.getErrorCode());
        }
        if (null == smartCardSyncMessageDTO.getInfo()) {
            return new MessageVO(SmartCardErrorCodeEnum.SYNCDATA_EMPTY_ERR.getErrorCode());
        }
        //直接保存一卡通推送的设备信息，用定时任务去解析数据
        iSmartCardSyncDataServiceClient.receiveSyncDataFromSmartCard(smartCardSyncMessageDTO);
        return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
    }





    /**
     * <desc>
     *     一卡通请求获取云端数据之后云端确认一卡通是否同步成功
     * <desc/>
     *
     * @param projectId 项目Id
     * @param syncSerailNos 同步流水号
     * @return
     * @author zhangs
     * @createDate 2017/10/31
     */
    @LoggerInfo(operateDesc = "一卡通请求获取云端数据之后云端确认一卡通是否同步成功")
    @RequestMapping(value = "cloudServer/confirmInfo", method = RequestMethod.POST)
    @ResponseBody
    public MessageVO confirmInfo(String projectId, String syncSerailNos) {
        try {
            //参数合法性判断
            if (StringUtils.isBlank(projectId)){
                return new MessageVO(SmartCardErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            if (StringUtils.isBlank(syncSerailNos)){
                return new MessageVO(SmartCardErrorCodeEnum.SERIALNUNBER_EMPTY_ERR.getErrorCode());
            }
            new Thread(() -> {
                iSmartCardSyncDataServiceClient.confirmInfo(projectId, syncSerailNos);
            }).start();
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw  new DataAccessException("【与旺龙一卡通对接_数据确认】一卡通同步数据确认失败", e);
        }
    }


    /**
    * <desc>
    *      根据同步参数获取推送给一卡通的同步数据.
    * </desc>
    *
    * @param  projectId 项目Id
    * @param  lastSyncSerailNos 上一次处理成功的同步流水号列表（逗号分隔）
     *@param dataType  业务数据类型
    * @return xx
    * @author LewisLouis
    * @createDate 2017-12-25
    */
    public MessageVO getSmartCardSyncMessage(String projectId, String lastSyncSerailNos, SmartCardSyncDataTypeEnum dataType){
        if (StringUtils.isBlank(projectId)) {
            return new MessageVO(SmartCardErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
        }
        SmartCardSyncGetPullDataDTO smartCardSyncGetPullDataDTO = new SmartCardSyncGetPullDataDTO();
        smartCardSyncGetPullDataDTO.setProjectId(projectId);
        smartCardSyncGetPullDataDTO.setDataType(dataType);
        smartCardSyncGetPullDataDTO.setLastSyncSerialNos(lastSyncSerailNos);
        List<SmartCardPullPO> resultList = iSmartCardSyncDataServiceClient.getNotSyncData(smartCardSyncGetPullDataDTO);

        SmartCardSyncMessageDTO smartCardSyncMessageDTO = new SmartCardSyncMessageDTO();
        smartCardSyncMessageDTO.setDataType(dataType.getType());
        smartCardSyncMessageDTO.setProjectId(projectId);
        smartCardSyncMessageDTO.setInfo(JSONArray.toJSONString(resultList));
        smartCardSyncMessageDTO.setSyncVersion( PlatformConstants.SMARTCARD_SYNC_VERSION);

        return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), smartCardSyncMessageDTO);
    }

}
